package src.cs2340.todoApp;

import java.util.Calendar;

import src.cs2340.todoApp.Date.Meredian;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class UserTaskListScreen extends Activity {
	// encapsulated shared variables
	private String username;
	private String password;
	private Account currAccount;
	private TableLayout table;
	private final int DATE_DIALOG_ID = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usertasklist);
		
		// Button for creating a new task
		Button newTask = (Button) findViewById(R.id.newTaskButton);
		// TODO: Next feature slice. Add map all locations functionality
		Button allLocs = (Button) findViewById(R.id.allLocsButton);
		
		
		// access the user information
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			username = extras.getString("username");
			password = extras.getString("password");
			
			// obtain the most updated version of the current account
			currAccount = new Account(username, password, "", "", Database.getInstance());
			currAccount = Database.getInstance().getUpdate(currAccount);
			
			table = (TableLayout) findViewById(R.id.taskList);
			updateTaskList(2,-1,null);
		}
		
		// create new task button listener
		newTask.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), CreateTaskScreen.class);
				intent.putExtra("username", username);
				intent.putExtra("password", password);
				finish();
				startActivity(intent);
			}
		});
		
		allLocs.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
				intent.putExtra("allLocs", true);
				intent.putExtra("username", username);
				intent.putExtra("password", password);
				startActivity(intent);
			}
		});
	}
	
	public void updateTaskList(int switchNum, int categoryPosition, Date d){
		switch(switchNum){
			case 0:
				table.removeAllViewsInLayout();
				TableRow titleRow1 = new TableRow(this);
				TextView taskName = new TextView(this);
				taskName.setText("Task Name");
				taskName.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				taskName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				
				Button datePicker = new Button(this);
				datePicker.setText("Pick a date");
				datePicker.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						showDialog(DATE_DIALOG_ID);
					}
				});
				
				TextView date = new TextView(this);
				date.setText("Due Date");
				date.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				date.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				titleRow1.addView(taskName);
//				titleRow1.addView(date);
				titleRow1.addView(datePicker);
				table.addView(titleRow1);
				
				// dynamically add items to the task list table
				for (TaskItem item : currAccount.tasksList.sortByDate(d)) {
					// add task items as rows
					TableRow row = new TableRow(this);
					final TaskItemCheckBox chkBox = new TaskItemCheckBox (this, item);
					chkBox.setText(item.toString());
					Log.e("checked", "item is checked case 0: " + item.isChecked());
					chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							chkBox.getTaskItem().setChecked(isChecked);
						}
					});
					Log.e("Checked", "Case 0 isChecked: " + item.isChecked());
					chkBox.setChecked(item.isChecked());
					chkBox.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					String dueDate = item.getDueDate().toString();
					TextView tv = new TextView(this);
					tv.setText(dueDate);
					tv.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					row.addView(chkBox);
					row.addView(tv);
					table.addView(row);
				}
				break;
			case 1:
				table.removeAllViewsInLayout();
				TableRow titleRow2 = new TableRow(this);
				TextView taskName1 = new TextView(this);
				taskName1.setText("Task Name");
				taskName1.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				taskName1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				Spinner spinner = new Spinner(this);
				String[] categories =  {"All","Personal", "School", "Work"};
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			            android.R.layout.simple_spinner_item, categories);
				spinner.setAdapter(adapter);
				spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent, View view,
							int position, long id) {
						if (position > 0) {
							updateTaskList(1,position-1,null);
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				
				TextView category = new TextView(this);
				category.setText("Category");
				category.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				category.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				titleRow2.addView(taskName1);
//				titleRow2.addView(category);
				titleRow2.addView(spinner);
				table.addView(titleRow2);
				
				// dynamically add items to the task list table
				for (TaskItem item : currAccount.tasksList.sortByCategory(categoryPosition)) {
					// add task items as rows
					TableRow row = new TableRow(this);
					final TaskItemCheckBox chkBox = new TaskItemCheckBox (this, item);
					chkBox.setText(item.toString());
					chkBox.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							chkBox.getTaskItem().setChecked(isChecked);
						}
					});
					Log.e("Checked", "Case 1 isChecked: " + item.isChecked());
					chkBox.setChecked(item.isChecked());
					String cat = item.getCategory();
					TextView tv = new TextView(this);
					tv.setText(cat);
					tv.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					row.addView(chkBox);
					row.addView(tv);
					table.addView(row);
				}
				break;
			case 2:
				table.removeAllViewsInLayout();
				TableRow titleRow3 = new TableRow(this);
				TextView taskName2 = new TextView(this);
				taskName2.setText("Task Name");
				taskName2.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				taskName2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				TextView date1 = new TextView(this);
				date1.setText("Due Date");
				date1.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				date1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				titleRow3.addView(taskName2);
				titleRow3.addView(date1);
				table.addView(titleRow3);
				// dynamically add items to the task list table
				for (TaskItem item : currAccount.tasksList.getTasks()) {
					// add task items as rows
					TableRow row = new TableRow(this);
					final TaskItemCheckBox chkBox = new TaskItemCheckBox (this, item);
					chkBox.setText(item.toString());
					chkBox.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							chkBox.getTaskItem().setChecked(isChecked);
						}
					});
					Log.e("Checked", "Case 2 isChecked: " + item.isChecked());
					chkBox.setChecked(item.isChecked());
					String dueDate = item.getDueDate().toString();
					TextView tv = new TextView(this);
					tv.setText(dueDate);
					tv.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					row.addView(chkBox);
					row.addView(tv);
					table.addView(row);
				}
				break;
			case 3:
				table.removeAllViewsInLayout();
				TableRow titleRow4 = new TableRow(this);
				TextView taskName3 = new TextView(this);
				taskName3.setText("Task Name");
				taskName3.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				taskName3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				TextView date2 = new TextView(this);
				date2.setText("Due Date");
				date2.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
				date2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
				titleRow4.addView(taskName3);
				titleRow4.addView(date2);
				table.addView(titleRow4);
				
				// dynamically add items to the task list table
				for (TaskItem item : currAccount.tasksList.getUncheckedTasks()) {
					// add task items as rows
					TableRow row = new TableRow(this);
					final TaskItemCheckBox chkBox = new TaskItemCheckBox (this, item);
					chkBox.setText(item.toString());
					chkBox.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							chkBox.getTaskItem().setChecked(isChecked);
						}
					});
					Log.e("Checked", "Case 2 isChecked: " + item.isChecked());
					chkBox.setChecked(item.isChecked());
					String dueDate = item.getDueDate().toString();
					TextView tv = new TextView(this);
					tv.setText(dueDate);
					tv.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 5));
					row.addView(chkBox);
					row.addView(tv);
					table.addView(row);
				}
				break;
		}
	}
	
	@Override
	public Dialog onCreateDialog(int id) {
		switch (id){
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, new OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					Date d = new Date(dayOfMonth, ++monthOfYear, year, 12, 0, Meredian.AM);
					updateTaskList(0, -1,d);
				}
			}, Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
		}
		return null;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.tasklistmenu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.sort_date:
	            updateTaskList(0,-1,null);
	            return true;
	        case R.id.sort_category:
	            updateTaskList(1,-1,null);
	            return true;
	        case R.id.showAllTasks:
	        	updateTaskList(2,-1,null);
	        	return true;
	        case R.id.showUncompTasks:
	        	updateTaskList(3,-1,null);
	        	return true;
	        	
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
}
